﻿
var map;
var id = 1;

//hàm này được gọi khi click button tìm kiếm


function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0)
            return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

//hàm này được gọi khi click chuột vô textbox tìm kiếm
function hidetext() {
    document.getElementById("tbSearch").value = '';
}

//hàm này để cập nhật lại treeview sau khi thêm xóa sửa
function updatetreeview() {
    var lat = document.getElementById('infoLatitude').innerHTML;
    var lon = document.getElementById('infoLongitude').innerHTML;
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("sidebar").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "update_treeview.aspx?lat=" + lat + "&lon=" + lon + "&type=" + 1, true);
    xmlhttp.send();
}
//Đi den trang chi tiết
function goToDetail() {
   
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("tbMessage").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "login.aspx", true);
    xmlhttp.send();
}
//Thêm/xóa/sửa
function InsertUpdateDelete(op, usernameOther, id, name, type, adress, lat, lon, status) {
    var xmlhttp;
    if (name == "") {
        document.getElementById("tbMessage").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("tbMessage").innerHTML = xmlhttp.responseText;
        }
    }
    //  alert(op + ' ' + status);
    xmlhttp.open("GET", "themxoasua.aspx?op=" + op + "&id=" + id + "&usernameOther=" + usernameOther + "&name=" + name + "&type=" + type + "&address=" + adress + "&lat=" + lat + "&long=" + lon + "&status=" + status, true);
    xmlhttp.send();
}

//Gọi hàm thêm xóa sửa (để thêm xóa sửa & share địa điểm cho tài khoản khác. Khi share địa điểm thì username = tên tài khoản cần share)
function didsomething(op, id, stat) {
    usernameOther = '';
    InsertUpdateDelete(op, usernameOther, id, document.getElementById("pname").value, document.getElementById("type").value, document.getElementById('address').innerHTML, document.getElementById('infoLatitude').innerHTML, document.getElementById('infoLongitude').innerHTML, stat);
    updatetreeview();
}

var geocoder = new google.maps.Geocoder();
function geocodePosition(pos) {
    geocoder.geocode({
        latLng: pos
    }, function (responses) {
        if (responses && responses.length > 0) {
            updateMarkerAddress(responses[0].formatted_address);
        } else {
            updateMarkerAddress('Cannot determine address at this location.');
        }
    });
}

function updateMarkerStatus(str) {
    document.getElementById('markerStatus').innerHTML = str;
}
function updateMarkerPosition(latLng) {
    document.getElementById('infoLatitude').innerHTML = [latLng.lat()];
    document.getElementById('infoLongitude').innerHTML = [latLng.lng()];
}
function updateMarkerAddress(str) {
    document.getElementById('address').innerHTML = str;
}

//Show marker

function movemaps(_zoom, posname, posid, poslat, poslong, type, status) {

    var mapLatLng = new google.maps.LatLng(poslat, poslong);
    map = new google.maps.Map(document.getElementById('content'), {
        zoom: _zoom,
        center: mapLatLng,
        mapTypeId: google.maps.MapTypeId.HYBRID
    });

    var latLng = new google.maps.LatLng(poslat, poslong);
    var marker = new google.maps.Marker({
        position: latLng,
        title: posname.toString(),
        map: map,
        draggable: false
    });

    updateMarkerStatus(posname);
    // Update current position info.
    updateMarkerPosition(latLng);
    geocodePosition(latLng);
    // Add dragging event listeners.
    google.maps.event.addListener(marker, 'dragstart', function () {
        updateMarkerAddress('Dragging...');
    });
    google.maps.event.addListener(marker, 'drag', function () {
        //updateMarkerStatus('Dragging...');
        updateMarkerPosition(marker.getPosition());
    });
    google.maps.event.addListener(marker, 'dragend', function () {

        geocodePosition(marker.getPosition());
    });

    //tao ra chuoi nội dung của infoWindow, là tài liệu html
    var contentString = '';
    var isLogin;
    isLogin = getCookie("userName");
    if (isLogin == null) {//chua dang nhap thi ko show gi ca ngoai thong báo
        contentString = 'You do not login yet !Please <a href="LoginandRegistery.aspx">login</a>';
    } else {
        if (status == "1") {//dia diem dc share
            contentString =
          '<div style="width:250px;">'
        + '<center>You are received shared place from a your friend</center>'
        + '<div style="width:100%;clear:both;float:left;margin-top:10px;">'
        + '<input type="hidden"   ID="pname" value="' + marker.getTitle().toString() + '"/> '
        + '<input type="hidden"   ID="type"  value="' + type + '"/> '
        + '    <button type="button"  style="width:150px;" onclick="didsomething(4,' + posid + ',' + status + ')">Accept</button> '
        + '    <button type="button"  style="width:150px;" onclick="didsomething(3,' + posid + ',' + status + ')">Not accept</button> '
        + '</div>'
        + '</div>';

        } else {
            contentString =
          '<div style="width:250px;">'
        + '<div style="width:100%;clear:both;float:left;">'
        + '<input type="text"  style="width:150px;" ID="pname" value="' + marker.getTitle().toString() + '"/> '
        + ' </div>'
        + '<div style="width:100%;clear:both;float:left;margin-top:10px;border-style:2px solid #234;">'
        + '  <hr/><a href="Detail.aspx?diadiemid=' + posid + '">Xem chi tiết địa điểm</a><br/> '
        + '</div>'
        + '<div style="width:100%;clear:both;float:left;margin-top:10px;">'
        + '   <hr/><button type="button" style="width:150px;" onclick="share(1,' + posid + ')">Share To</button> '
        + '    <input type="text" ID="sname" style="width:150px;"/>'
        + '</div>'
        + '</div>';
        }
    }
    //  alert(contentString);

    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    google.maps.event.addListener(marker, 'click', function () {
        infowindow.open(map, marker);
    });

}

function selectPlace() {
    google.maps.event.clearListeners(map, 'click');
    google.maps.event.addListenerOnce(map, 'click', function (event) {
        movemaps('I am University of Science Student', 4, event.latLng.lat(), event.latLng.lng(), 1, 0);
    });

}
// Onload handler to fire off the app.
//google.maps.event.addDomListener(window, 'load', initialize);

//Gọi hàm thêm xóa sửa (để thêm xóa sửa & share địa điểm cho tài khoản khác. Khi share địa điểm thì usernameOther = tên tài khoản cần share)
function share(op, id) {
    usernameOther = document.getElementById("sname").value;
    if (usernameOther.length != 0)
        InsertUpdateDelete(op, usernameOther, id, document.getElementById("pname").value, document.getElementById("type").value, document.getElementById('address').innerHTML, document.getElementById('infoLatitude').innerHTML, document.getElementById('infoLongitude').innerHTML, 1);

}